<script >
import {
    userGetAllAddressService,
    userUpdateAddressService,
    userSetDefaultAddressService,
    userCancelDefaultAddressService,
    userAddAddressService
} from "@/api/user/UserService";
export default {
    data() {
        return {
            userInfo: [],
            value: true,
            form: {
                detailAddr: "",
                phone: "",
                receiverName: "",
                urbanAddr: "",
                userId: "",
            },
            addressList: [],
            flag: false,
        }
    },
    methods: {
        // 封装时间返回对象
        getFormatDate(time) {
            const date = new Date(time);
            const year = date.getFullYear();
            const month = (date.getMonth() + 1).toString().padStart(2, '0');
            const day = date.getDate().toString().padStart(2, '0');
            return `${year}-${month}-${day}`;
        },
        async initAddress() {
            let result = await userGetAllAddressService();
            if(result.code === 200) {
                this.addressList = result.data;
            }
            
        },
        deleteAddress() {
            this.$confirm('此操作将永久删除该地址, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        addAddress() {
            console.log(this.form)
            this.flag = true;
            this.form = JSON.parse(JSON.stringify( {
                detailAddr: "",
                phone: "",
                receiverName: "",
                urbanAddr: "",
                userId: this.userInfo.id,
            }))
            console.log(this.form)
        },
        editAddress(row) {
            this.flag = true;
            this.form = JSON.parse(JSON.stringify(row));
        },
        async save() {
            if(this.form.userId == null) {
                let result = await userUpdateAddressService(this.form);
                if(result.code === 200) {
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                    this.flag = false;
                    await this.initAddress();
                } else {
                    this.$message({
                        message: '修改失败',
                        type: 'error'
                    });
                }
            } else {
                let result = await userAddAddressService(this.form);
                if(result.code === 200) {
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    });
                    this.flag = false;
                    await this.initAddress();
                } else {
                    this.$message({
                        message: result.message,
                        type: 'error'
                    });
                }
            }
        },
        /**
         * 设置默认地址
         * @param row
         */
        async changeStatus(row) {
            console.log(row.defaultValue);
            if(row.defaultValue) {
                let result = await userSetDefaultAddressService(row.id);
                if(result.code === 200) {
                    this.$message({
                        message: "设置默认地址成功",
                        type: "success"
                    })
                    await this.initAddress();
                } else {
                    this.$message({
                        message: result.message,
                        type: "error"
                    })
                }
            } else {
                let result = await userCancelDefaultAddressService(row.id);
                if(result.code === 200) {
                    this.$message({
                        message: "取消默认地址成功",
                        type: "success"
                    })
                    await this.initAddress();
                } else {
                    this.$message({
                        message: result.message,
                        type: "error"
                    })
                }
            }
        }
    },
    created() {
        this.userInfo = JSON.parse(JSON.stringify(this.$store.getters.userInfo));
        this.initAddress();
    }
}
</script>

<template>
    <div class="UserModel">
        <div class="u_top">
            <div>收货地址</div>
            <div class="four">
                <el-button type="primary" icon="el-icon-edit" @click="addAddress">新增地址</el-button>
            </div>
        </div>
        <div class="u_body">
            <div class="adMask" v-show="flag">
                <div class="contain">
                    <el-form label-position="left" label-width="100px" ref="form">
                        <el-form-item label="收件人姓名">
                            <el-input v-model="form.receiverName"></el-input>
                        </el-form-item>
                        <el-form-item label="收件人电话">
                            <el-input v-model="form.phone"></el-input>
                        </el-form-item>
                        <el-form-item label="省市区地址">
                            <el-input v-model="form.urbanAddr"></el-input>
                        </el-form-item>
                        <el-form-item label="详细地址">
                            <el-input v-model="form.detailAddr"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="save" >保存</el-button>
                            <el-button @click="flag = false">返回</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="address" v-for="item in addressList" :key="item.id">
                <div class="add_username">
                    <span>姓名：</span>
                    <span>{{ item.receiverName }}</span>
                </div>
                <div class="add_phone">
                    <span>电话：</span>
                    <span>{{ item.phone }}</span>
                </div>
                <div class="addr">
                    <span>地址：</span>
                    <span>{{ item.urbanAddr }}</span>
                </div>
                <div class="operation">
                    <div class="defaultValue">
                        <div>默认地址：</div>
                        <el-switch
                            @change="changeStatus(item)"
                            v-model="item.defaultValue"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                        </el-switch>
                    </div>
                    <div class="opera">
                        <el-button type="primary" icon="el-icon-edit" @click="editAddress(item)" circle></el-button>
                        <el-button type="danger" icon="el-icon-delete" @click="deleteAddress" circle></el-button>
                    </div>
                </div>
            </div>

        </div>
    </div>
</template>

<style scoped lang="scss">
.UserModel {
    width: 72%;
    height: auto;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 0 7px 3px rgb(212 206 206);
    background-color: #fff;
    overflow: hidden;
    .u_top {
        width: 100%;
        height: 50px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 5px 5px 5px 10px;
        font-weight: 800;
        div {
            font-size: 18px;
        }
        .el-button {
            font-weight: 800;
            transition: all .5s ease;
            color: #a4c1fa;
            transform: scale(0.88);
            background-color: rgba(238, 245, 254,0.99);
        }
        .el-button:hover {
            color: #fff;
            background-color: rgba(100, 156, 248);
        }
    }
    .u_body {
        width: 100%;
        height: auto;
        display: grid;
        padding: 10px;
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
        grid-template-rows: repeat(3, 130px);
        position: relative;
        .adMask {
            position: absolute;
            background-color: rgba(235, 235, 244,0.89);
            width: 100%;
            height: 100%;
            z-index: 99;
            left: 0;
            top: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            .contain {
                display: flex;
                justify-content: center;
                align-items: center;
                width: 60%;
                height: 100%;
                background-color: rgba(155, 204, 232, 0.77);
            }
        }
        .address {
            padding: 10px;
            width: 90%;
            height: 130px;
            background-image: linear-gradient(to top, #e6e9f0 0%, #eef1f5 100%);
            border-radius: 10px;
            display: flex;
            gap: 5px;
            flex-direction: column;
            .add_username, .add_phone, .addr {
                display: flex;
                gap: 10px;
                span:nth-child(1) {
                    font-weight: 800;
                }
                span:nth-child(2) {
                    font-weight: 800;
                }
            }
            .operation {
                width: 100%;
                display: flex;
                justify-content: space-between;
                align-items: center;
                .defaultValue {
                    display: flex;
                    align-items: center;
                    div {
                        font-weight: 800;
                    }
                }
                .opera {
                    .el-button {
                        transform: scale(0.8);
                    }
                }
            }
        }
    }
}
</style>
